package com.woparker.kiosk.utils;

import android.content.Context;


import com.woparker.kiosk.bean.DateBean;

import java.io.DataOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by yuanxd on 2017/11/6 0006.
 */

public class TimeUtil {

    //时间格式
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd\n HH:mm:ss");
    private static final SimpleDateFormat timeFormat1 = new SimpleDateFormat("HH:mm:ss");
    public static final SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timeFormat3 = new SimpleDateFormat("HH");
    private static final SimpleDateFormat timeFormat4 = new SimpleDateFormat("yyyyMMdd.HHmmss");


    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay, Context context) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        String dayAfter = specifiedDay;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);
        if (System.currentTimeMillis() <= c.getTime().getTime()) {
            DialogUtil.showDialogWarning(context);
            return specifiedDay;
        }

        dayAfter = new SimpleDateFormat("yyyy-MM-dd")
                .format(c.getTime());
        return dayAfter;
    }


    /**
     * 获得指定日期的前一天
     *
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {//可以用new Date().toLocalString()传递参数
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
                .getTime());
        return dayBefore;
    }


    public static String currentTimePeriod() {
        String str = "";
        int t = Integer.valueOf(timeFormat3.format(System.currentTimeMillis()));
        // int timeStr = Integer.valueOf(timeFormat3.format(System.currentTimeMillis()));
        int timeStr = t - 1;
        int time1 = 0;
        int time2 = 0;
        if (timeStr % 2 == 0) {
            time1 = timeStr;
            time2 = timeStr + 2;

        } else if (timeStr % 2 == 1) {
            System.out.println("1111111");
            time1 = timeStr - 1;
            time2 = timeStr + 1;
        }
        str = numStr(time1) + ":00" + "-" + numStr(time2) + ":00";
        return str;
    }

    private static String numStr(int x) {
        String str = x + "";
        if (String.valueOf(x).length() == 1) {
            str = "0" + x;
        }
        return str;
    }

    public static String timeFormat2(long time) {
        return timeFormat2.format(time);
    }

    /**
     * 获取某天几时的时间戳
     * @param
     * @return
     */
    public static long oneayTime(String s, int time) {
        int year;
        int month;
        int day;
        long dayTime = 0;

        year = setyear(s);
        month = setmonth(s);
        day = setday(s);

        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);  // 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, time);  // 设置为1 ,当前日期既为该天的几时
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        //纽约时间戳转北京时间减去8小时
        dayTime = calendar.getTimeInMillis() / 1000 - 8 * 3600;
        return dayTime;
    }


    /**
     * 获取该时间：日
     *
     * @param specifiedDay
     * @return
     */
    public static int setday(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);

        return day;
    }

    /**
     * 获取该时间：年
     *
     * @param specifiedDay
     * @return
     */
    public static int setyear(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int year = c.get(Calendar.YEAR);

        return year;
    }

    /**
     * 获取该时间：月
     *
     * @param specifiedDay
     * @return
     */
    public static int setmonth(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int month = c.get(Calendar.MONTH);

        return month;
    }


    public static int getAutoTime(String autoTimeStr) {
        int time = 0;
        if (autoTimeStr.equals("不启用")) {
            time = 0;
        } else if (autoTimeStr.equals("30秒")) {
            time = 30;
        } else if (autoTimeStr.equals("60秒")) {
            time = 60;
        } else if (autoTimeStr.equals("90秒")) {
            time = 90;
        } else if (autoTimeStr.equals("120秒")) {
            time = 120;
        } else if (autoTimeStr.equals("240秒")) {
            time = 240;
        }
        return time;
    }


    public static DateBean getData(long time) {
        Calendar CD=Calendar.getInstance();
        if(time != 0){
            String timeStr = longToStrings(time);
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = sdf.parse(timeStr);
                CD=Calendar.getInstance();
                CD.setTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        int YY = CD.get(Calendar.YEAR);
        int MM = CD.get(Calendar.MONTH) + 1;
        int DD = CD.get(Calendar.DATE);
        int HH = CD.get(Calendar.HOUR_OF_DAY);
        int NN = CD.get(Calendar.MINUTE);
        int SS = CD.get(Calendar.SECOND);
        int dayOfWeek = CD.get(Calendar.DAY_OF_WEEK);
        String dayWeekStr = "";
        switch (dayOfWeek) {
            case 1:
                dayWeekStr = "星期日";
                break;
            case 2:
                dayWeekStr = "星期一";
                break;
            case 3:
                dayWeekStr = "星期二";
                break;
            case 4:
                dayWeekStr = "星期三";
                break;
            case 5:
                dayWeekStr = "星期四";
                break;
            case 6:
                dayWeekStr = "星期五";
                break;
            case 7:
                dayWeekStr = "星期六";
                break;
        }

        StringBuilder dateBuilder = new StringBuilder();
        dateBuilder.append(YY);
        dateBuilder.append("-");
        dateBuilder.append(MM);
        dateBuilder.append("-");
        dateBuilder.append(DD);

        StringBuilder timeBuilder = new StringBuilder();
        timeBuilder.append(HH);
        timeBuilder.append(":");
        timeBuilder.append(NN);
        timeBuilder.append(":");
        timeBuilder.append(SS);

        return new DateBean(dateBuilder.toString(), timeBuilder.toString(), dayWeekStr);
    }


    /**
     * 设置系统时间
     */
    public static void setSystemTime(long utcTime) {
        try {
            Process process = Runtime.getRuntime().exec("su");
            String datetime = timeFormat4.format(new Date(utcTime + 3600 * 1000 * 8)); //测试的设置的时间【时间格式 yyyyMMdd.HHmmss】
            DataOutputStream os = new DataOutputStream(process
                    .getOutputStream());
            os.writeBytes("setprop persist.sys.timezone GMT\n");
            os.writeBytes("/system/bin/date -s "
                    + datetime + "\n");
            os.writeBytes("clock -w\n");
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Date stringToDate(String strTime, String formatType) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     *
     * @param currentTime 要转换的long类型的时间
     * @param formatType 要转换的string类型的时间格式
     * @return
     */
    public static String longToString(long currentTime, String formatType){
        //mill为你龙类型的时间戳
        Date date = new Date(currentTime);
        String strs = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatType, Locale.CHINA);
            strs = sdf.format(date);
            System.out.println(strs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }


    public static String longToStrings(long longTime){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));//**TimeZone时区，加上这句话就解决啦**
        String time = sd.format(new Date(longTime));//ps.将毫秒转化为时分秒格式
        return time;
    }
    
    public static long stringsToLong(String longTime){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));//**TimeZone时区，加上这句话就解决啦**
        long time = 0;
        try {
            time = sd.parse(longTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

}
